from forest.api import Command, get_tool

Users = get_tool('users.Users')

class CreateUsers(Command):
    ''' Creates extra users specified in the relevant resource.
    '''

    __register__ = 'commands.setup.CreateUsers'

    requires_config = ['users']

    def run(self, config):
        self.args = config.get('users', False)

        if not self.args:
            return

        users = Users(config.get_install_root())

        for ud in self.args:
            kwargs = ud
            if not users.createUser(ud.get('name'), **kwargs):
                print "User %s already exists, not creating." % ud.get('name', '')


class CreateGroups(Command):
    ''' Creates extra groups based on the groups resource definition.
    '''

    __register__ = 'commands.setup.CreateGroups'

    requires_config = ['groups']

    def run(self, config):
        self.args = config.get('groups', False)

        if not self.args:
            return

        users = Users(config.get_install_root())

        for gd in self.args :
            kwargs = gd
            if not users.createGroup(gd.get('name'), **kwargs):
                print "Group %s already exists, not creating." % gd.name
